import React from "react";
import { dispatch } from "./useDispatch";
import { useSelector } from "./useSelector";
import { IDispatch, ISelector } from "./types";

const connect = (
  mapToProps?: ISelector,
  mapToDispatch?: (dispatch: IDispatch) => any
) => {
  return (Comp: React.FC<any> | React.ComponentClass) => {
    return (props: any) => {
      const mapProps = mapToProps ? useSelector(mapToProps) || {} : {};
      const mapDispatch = mapToDispatch
        ? mapToDispatch(dispatch) || { dispatch }
        : { dispatch };
      return <Comp {...props} {...mapProps} {...mapDispatch} />;
    };
  };
};

export default connect;
